﻿using Newtonsoft.Json;
using ReferenceTemplate.Models;
using ReferenceTemplate.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace ReferenceTemplate.WindowsPhone.Services
{
    public class DataService : IDataService
    {
        private static string dataFile = "ms-appx:///Content/config.json";
        public async Task<AppData> LoadData()
        {
            AppData data = null;

            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(dataFile));

            if (file != null)
            {
                string json = "";
                using (StreamReader streamReader = new StreamReader(await file.OpenStreamForReadAsync()))
                {
                    json = streamReader.ReadToEnd();
                }

                data = JsonConvert.DeserializeObject<AppData>(json);
            }

            return data;
        }
    }
}
